<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Home\Controller;
use Think\Controller;

class ArticleController extends BaseController {

	public function shengshi(){
		$shi = $_GET['shi'];
		$shi = iconv("gbk","UTF-8",$shi);
		$this->assign('shi',$shi);
		$this->display('index');
	}

	/**
	 * 根据ID获取对应的文章
	 */
	public function index($id = null){
		if($this->getmatypeid($id)){
			$this->showArticle($id);
			$this->display('Index/article');

		}else{
			echo "此码被关闭，请联系管理员！";
		}
	}

	public function index1($id = null){
		if($this->getmatypeid($id)){
			$this->showArticle($id);
			$this->display('Index/article1');
		}else{
			echo "此码被关闭，请联系管理员！";
		}
	}

	/**
	 * 根据ID获取内容信息
	 */
	protected function showArticle($id = null){
		if(!empty($id)){
			$article = D(C('TABLE_ARTICLE'));
			$articleReuslt = $article->find($id);
			if($articleReuslt['state']){
				if(!empty(trim($articleReuslt['jumplink']))){
					echo "<script>location.href='".$articleReuslt['jumplink']."'</script>";
				}
				$xg_articlelist = $this->get_menuid_articlelist($articleReuslt['menuid']);
				$this->assign('article', $articleReuslt);
				//相关文档
				$this->assign('other_article', $xg_articlelist);
			}else{
				$this->alerterror('本条数据被关闭,请联系管理员！');
			}

		}else{
			echo "<script>alert('缺少参数！');</script>";
		}
	}

	/**
	 * 根据MUENUID获取所有文档
	 */
	protected function get_menuid_articlelist($menuid = null){
		$article = D(C('TABLE_ARTICLE'));
		$res = $article->where(array('menuid' => $menuid, 'state' => 1))->select();
		return $res;
	}

	/**
	 * 根据文章ID获取哪个码的ID
	 */
	protected function getmatypeid($typeid){
		$article = D(C('TABLE_ARTICLE'));
		$articleReuslt = $article->find($typeid);
		$id = $articleReuslt['typeid'];
		$type = D(C('TABLE_TYPE'));
		$r = $type->find($id);
		return $r['state'];
	}

	//保存评论
	public function savemessage(){
		$userinfo = $_COOKIE['user_userinfo'];
		if(empty($userinfo)){
			//请使用微信登陆
			$messages = array('ret'=>-2);
			//header("Location:".C('LOGINADDRES').getURL());
		}else{
			$r = request_get(C('APIADDRES1')."/getuserinfo?token=".$userinfo);
			if($r['ret']==1){
				$usernickname = $r['data']['nickname'];
				$userheadimg = $r['data']['headimgurl'];
				$usersex = $r['data']['sex'];
				$articleid = I('post.id');
				$ucontent = trim(I('post.ucontent'));
				$cdate = date("Y-m-d H:i:m");
				$useropenid = $userinfo;
				$type = I('post.type');

				$comment = D(C('TABLE_COMMENT'));
				$data['useropenid'] = $useropenid;
				$data['ucontent'] = $ucontent;
				$data['usernickname'] = $usernickname;
				$data['userheadimg'] = $userheadimg;
				$data['usersex'] = $usersex;
				$data['cdate'] = $cdate;
				if($type==1){
					$data['articleid'] = $articleid;
				}else if($type=2){
					$data['resourceid'] = $articleid;
				}
				$data['type'] = $type;

				$res = $comment->add($data);
				if($res){
					//提交成功
					$messages = array('ret'=>1);
				}else{
					//提交失败
					$messages = array('ret'=>0);
				}
			}else{
				//获取个人信息出错
				$messages = array('ret'=>-1);
			}
		}
		echo json_encode($messages);

	}

	//获取评论
	public function getmessages(){
		$result = null;
		$articleid = I('post.id');
		$type = I('post.type');
		$comment = D(C('TABLE_COMMENT'));
		if($type==1){
			$where['articleid'] = $articleid;
		}else if($type==2){
			$where['resourceid'] = $articleid;
		}
		$where['ustatus'] = 1;
		$where['type'] = $type;
		$res = $comment->where($where)->order('id desc')->select();
		if($res){
			foreach($res as $v){
				$result .= "<div class=\"list-box clearfix\">"
						. "       <div class=\"img-box\"><img src=\"".$v['userheadimg']."\"></div>"
						. "       <div class=\"messages-content\">"
						. "         <h4>".$v['usernickname']."</h4>"
						. "         <em>".$v['cdate']."</em>"
						. "         <p>".$v['ucontent']."</p>"
						. "       </div>"
						. "     </div>"
						."";
			}
		}
		echo $result;
	}

}